package com.witte.serviceImp;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.witte.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.witte.dao.CustomerMapper;
import com.witte.dao.OrdersMapper;
import com.witte.dao.ProductMapper;
import com.witte.dao.UserMapper;
import com.witte.service.OrdersService;

@Service
public class OrderServiceImp implements OrdersService{

	@Autowired
	OrdersMapper ordersMapper;
	
	@Autowired
	CustomerMapper customerMapper;
	
	@Autowired(required=false)
	ProductMapper productMapper;
	
	//doesn't use this methods
	@Override
	public List<Orders> getAllOrders() {
		// TODO Auto-generated method stub
		return ordersMapper.selectAllOrders();
	}

	@Override
	public int getSumMoney() {
		// TODO Auto-generated method stub
		return ordersMapper.selectSumMoney();
	}

	@Override
	public List<CusNOrdProN> getAllCOP() {
		// TODO Auto-generated method stub
		return ordersMapper.selectCusAllMoney();//
	}

	//divided the name to find customer name ,product name ,order state, money
	@Override
	public List<CusNOrdProN> getCOPByCusName(String name) {
		// TODO Auto-generated method stub
		Customer ct = customerMapper.selectByname(name);
		List<Orders> order = ordersMapper.selectByCId(ct.getNo());
		System.out.println(order.size());
		//List<Product> plist = new ArrayList<Product>();
		List<CusNOrdProN> cpnlist = new ArrayList<CusNOrdProN>();
		for(Orders i:order) {
			Product p = productMapper.selectByPrimaryKey(i.getProductid());
			//plist.add(p);
			CusNOrdProN cpn = new CusNOrdProN();
			cpn.setCusName(ct.getName());
			cpn.setMoney(i.getMoney());
			cpn.setProName(p.getProductname());
			cpn.setState(i.getState());
			cpnlist.add(cpn);
		}
		return cpnlist;
	}

	@Override
	public Orders getOrderByNo() {
		// TODO Auto-generated method stub
		List<Orders> len = this.getAllOrders();
		int length = len.size();
		String str = new Random(length)+"";
		return ordersMapper.selectByPrimaryKey(str);
	}

	@Override
	public List<Orders> selectForPage(Integer beginPage, Integer pageSize) {
		int begin = (beginPage-1)*pageSize;
		return ordersMapper.selectForPage(begin,pageSize);
	}

	@Override
	public int getCount() {
		return ordersMapper.getAllCount();
	}
	
	@Override
	public List<Integer> getAllLost() {
		// TODO Auto-generated method stub
		return ordersMapper.selectNoByOverTime();
	}

}
